package com.yunbiao.ybbusstopplate.refactoring;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.library_hy_advert.system.Cache;
import com.yunbiao.ybbusstopplate.BuildConfig;
import com.yunbiao.ybbusstopplate.refactoring.ui.fengxian.BASHITONGParser;
import com.yunbiao.ybbusstopplate.refactoring.agreement.base.Parser;
import com.yunbiao.ybbusstopplate.refactoring.ui.dalian.ZHIDAParser;
import com.yunbiao.ybbusstopplate.ui.activity.FlavorType;
import com.yunbiao.ybbusstopplate.refactoring.ui.fengxian.TONGDA2020Parser;
import com.yunbiao.ybbusstopplate.ui.activity.FlavorType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Config {

//    public static final String PORT = "/dev/ttyS2";
    public static final String PORT;

    static {
        if (TextUtils.equals("3566X", Build.MODEL)) {
            PORT = "/dev/ttySMT6";
        } else {
            PORT = "/dev/ttyS2";
        }
    }

//        Build.MODEL  3566X
//        Build.DEVICE  rk3566

    public static void setAgreement(Agreement agreement){
        Cache.setInt(Key.AGREEMENT.name(),agreement.ordinal());
    }

    private static final String TAG = "Config";
    public static Agreement getAgreement(){
        int defaultAgreementOrdinal;
        if(BuildConfig.FLAVOR_TYPE == FlavorType.DALIAN){
            defaultAgreementOrdinal = Agreement.ZHIDA.ordinal();
        } else if(BuildConfig.FLAVOR_TYPE == FlavorType.FENGXIAN){
            defaultAgreementOrdinal = Agreement.BASHITONG.ordinal();
        } else {
            defaultAgreementOrdinal = 0;
        }
        int ordinal = Cache.getInt(Key.AGREEMENT.name(),defaultAgreementOrdinal);
        Agreement agreement = Agreement.ordinalOf(ordinal);
        if(agreement == null){
            Cache.setInt(Key.AGREEMENT.name(), Agreement.BASHITONG.ordinal());
            agreement = Agreement.BASHITONG;
        }
        return agreement;
    }

    public static DeviceType getDeviceType(){
        int deviceTypeOrdinal = Cache.getInt(Config.Key.DEVICE_TYPE.name(),DeviceType.NODE_SITE_PLATE.ordinal());
        return DeviceType.ordinalOf(deviceTypeOrdinal);
    }

    public static void setDeviceType(DeviceType deviceType){
        Cache.setInt(Key.DEVICE_TYPE.name(), deviceType.ordinal());
    }

    public static String getSourceAddress(){
        if (getDeviceType().equals(DeviceType.HEAD_INFORMATION_SCREEN)) {
            return "20";
        } else {
            return "21";
        }
    }

    public enum DeviceType {
        NODE_SITE_PLATE("节站屏"),
        HEAD_INFORMATION_SCREEN("头部信息屏");

        private final String name;

        DeviceType(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @NonNull
        @Override
        public String toString() {
            return name;
        }

        public static DeviceType ordinalOf(int ordinal){
            DeviceType deviceType = null;
            for (DeviceType value : values()) {
                if(value.ordinal() == ordinal){
                    deviceType = value;
                    break;
                }
            }
            return deviceType;
        }


        public static List<DeviceType> list(){
            return new ArrayList<>(Arrays.asList(values()));
        }
    }

    public enum Key {
        DEVICE_TYPE,
        AGREEMENT,

        COMPANY_ICON,
        COMPANY_NAME,
        PHONE_NUMBER,
        START_START_END_TIME,
        END_START_END_TIME

        ;
    }

    public static class Default {
        public static String PHONE_NUMBER = "968600";
    }

    public enum Agreement {

        ZHIDA(19200,"智达", ZHIDAParser.class,FlavorType.DALIAN),
        BASHITONG(9600,"巴士通", BASHITONGParser.class,FlavorType.FENGXIAN),
        TONGDA2020(38400,"通达2020", TONGDA2020Parser.class,FlavorType.FENGXIAN),
        ;

        private final int baudRate;
        private final String name;
        private final int flavorType;
        private final Class<? extends Parser> parserClazz;
        private Parser parser;
        Agreement(int baudRate, String name,Class<? extends Parser> parserClazz,int flavorType) {
            this.baudRate = baudRate;
            this.name = name;
            this.parserClazz = parserClazz;
            this.flavorType = flavorType;
        }

        public Parser getParser() {
            return parser;
        }

        public Class<? extends Parser> getParserClazz() {
            return parserClazz;
        }

        public int getBaudRate() {
            return baudRate;
        }

        public String getName() {
            return name;
        }

        public int getFlavorType() {
            return flavorType;
        }

        public void initParser() throws Exception{
            this.parser = parserClazz.getDeclaredConstructor().newInstance();
        }

        @NonNull
        @Override
        public String toString() {
            return getName();
        }

        public static Agreement ordinalOf(int ordinal){
            Agreement agreement = null;
            for (Agreement value : values()) {
                if(value.ordinal() == ordinal){
                    agreement = value;
                    break;
                }
            }
            return agreement;
        }

        public static List<Agreement> list(){
            List<Agreement> agreements = new ArrayList<>();
            for (Agreement value : values()) {
                if(value.getFlavorType() == BuildConfig.FLAVOR_TYPE){
                    agreements.add(value);
                }
            }
            return agreements;
        }
    }

}
